<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\controller\Admin;
use app\common\model\Topup as TopupModel;

class Topup extends Admin {
  protected $Topup;
  protected $TopupList;
  public function _initialize() {
		parent::_initialize();
    $this->Topup = model('Topup');
    $this->TopupList = model('TopupList');
	}
  public function index() {
    $map  = array('status' => array('gt', -1));
    $list = $this->Topup->where($map)->order('sort asc,id desc')->paginate(10);

    Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->assign('list', $list);
		$this->setMeta('优惠券管理');
		return $this->fetch();
	}

  public function add() {
    if (IS_POST) {
      	$result = $this->Topup->change();
      if($result){
        $this->success('添加成功！',  url('admin/Topup/index'));
      }else{
        $this->error('添加失败！');
      }
    } else {
			$data = array(
        'info'    => array('status' => 1,'sort'=>1),
				'keyList' => $this->Topup->addfield,
			);
			$this->assign($data);
			$this->setMeta("添加分类");
			return $this->fetch('public/edit');
		}
  }

  public function edit($id) {
		if (!$id) {
			return $this->error("非法操作！");
		}
		if (IS_POST) {
			$result = $this->Topup->change();
			if ($result) {
				return $this->success("编辑成功！", url('admin/Topup/index'));
			} else {
				return $this->error("编辑失败！");
			}
		} else {
			$info = $this->Topup->where(array('id' => $id))->find();
			$data = array(
				'info'    => $info,
				'keyList' => $this->Topup->addfield,
			);
			$this->assign($data);
			$this->setMeta('编辑分类');
			return $this->fetch('public/edit');
		}
	}

  public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$result = $this->Topup->where(array('id' => array('IN', $id)))->delete();
		if ($result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}


  public function lists($id){
    if (!$id) {
			return $this->error("非法操作！");
		}
    $list = $this->TopupList->where(['topup_id'=>$id])->paginate(20);

    $this->assign('topup_id', $id);
		$this->assign('list', $list);
		$this->setMeta('充值卡列表');
		return $this->fetch();
  }


public function addcode($id) {
    if (!$id) {
      return $this->error("非法操作！");
    }
  if (IS_POST) {
    $num        = input('post.num');
    $topup_id      = input('post.topup_id');

    if($num<1){
      $this->error('数量填写错误！');
    }
    $info  =  $this->Topup->find($topup_id);

    $timePrefix = date('md');
    for ($i=0; $i < $num ; $i++) {
      $data[$i]['code']  = rand_string(4, 3).$timePrefix.rand_number(4);
      $data[$i]['topup_id']  = $topup_id;
      $data[$i]['sum'] = $info['sum'];
    }

    $result = $this->TopupList->saveAll($data);
    if($result){
      $this->success('添加成功！',  url('admin/Topup/lists',array('id'=>$id)));
    }else{
      $this->error('添加失败！');
    }
  } else {
    $data = array(
      'info'    => array('topup_id' => $id),
      'keyList' => $this->Topup->addcode,
    );

    $this->assign($data);
    $this->setMeta("生成充值卡码");
    return $this->fetch('public/edit');
  }
}

public function delcode() {
  $id = $this->getArrayParam('id');
  if (empty($id)) {
    return $this->error("非法操作！");
  }
  $result = $this->TopupList->where(array('id' => array('IN', $id)))->delete();
  if ($result) {
    return $this->success("删除成功！");
  } else {
    return $this->error("删除失败！");
  }
}
/**
 * 充值记录
 */
  public function loglist(){
    $list = model('topupLog')->order('id desc')->paginate(10);
    $this->assign('list', $list);
    $this->setMeta("充值记录");
    return $this->fetch();
  }
  public function logdel() {
    $id = $this->getArrayParam('id');
    if (empty($id)) {
      return $this->error("请选择要删除的数据！");
    }
    $result = model('topupLog')->where(array('id' => array('IN', $id)))->delete();
    if ($result) {
      return $this->success("删除成功！");
    } else {
      return $this->error("删除失败！");
    }
  }

}

 ?>
